package cn.pack;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.DB.DBProcess;
import cn.entity.Admin;
import cn.entity.User;
import cn.entity.goods;

/**
 * Servlet implementation class Check
 */
public class Check extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Check() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if ("login".equals(action)) {

			/**
			 * 用户登录
			 */
			String admin_name = request.getParameter("username");
			String admin_pwd = request.getParameter("password");

			/**
			 * 从cookie里面获取用户信息
			 */
			if (admin_name == null) {
				Cookie[] cks = request.getCookies();
				for (Cookie ck : cks) {
					if (ck.getValue().equals(admin_name)) {
						admin_name = ck.getValue();
					}
					if (ck.getValue().equals(admin_pwd)) {
						admin_pwd = ck.getValue();
					}
				}
			}
			/**
			 * 验证用户身份
			 */
			User adm = new User();
			try {
				if (adm.validate(admin_name, admin_pwd)) {
					request.getSession().setAttribute("loginName", admin_name);
					Cookie ck = new Cookie("loginName", admin_name);
					ck.setMaxAge(10000);
					response.addCookie(ck);

					request.getSession().setAttribute("passwd", admin_pwd);
					ck = new Cookie("passwd", admin_pwd);
					ck.setMaxAge(10000);
					response.addCookie(ck);

					// 添加所有商品列表信息
					DBProcess db = new DBProcess();
					List<goods> goodsList = db.getgoodsList();
					request.getSession().setAttribute("Goodslist", goodsList);

					response.sendRedirect("index.jsp");

				} else {

					request.getSession().setAttribute("errMsg", "登录不正确...");

					response.sendRedirect("login.jsp");

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else if ("admin".equals(action)) {
			/**
			 * 管理者登录
			 */

			String admin_name = request.getParameter("username");
			String admin_pwd = request.getParameter("password");

			/**
			 * 从cookie里面获取用户信息
			 */
			if (admin_name == null) {
				Cookie[] cks = request.getCookies();
				for (Cookie ck : cks) {
					if (ck.getValue().equals(admin_name)) {
						admin_name = ck.getValue();
					}
					if (ck.getValue().equals(admin_pwd)) {
						admin_pwd = ck.getValue();
					}
				}
			}
			/**
			 * 验证用户身份
			 */
			Admin adm = new Admin();
			try {
				if (adm.validate(admin_name, admin_pwd)) {
					request.getSession().setAttribute("username", admin_name);
					Cookie ck = new Cookie("username", admin_name);
					ck.setMaxAge(10000);
					response.addCookie(ck);

					request.getSession().setAttribute("password", admin_pwd);
					ck = new Cookie("password", admin_pwd);
					ck.setMaxAge(10000);
					response.addCookie(ck);
					response.sendRedirect("Ctrl?action=list");
					// response.sendRedirect("list.jsp");

				} else {

					request.getSession().setAttribute("errMsg", "登录不正确...");

					response.sendRedirect("login.jsp");

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
